/*
 * Copyright 2008-2010 GP Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * XlsRow.java | Created on Sep 29, 2010
 */
package org.gpsoftware.xlsMerge;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

/**
 * The MS Excel row implementation
 *
 * @author Paul Augustine C. Mendoza
 */
public class XlsRow implements Row<HSSFRow>, Comparable {

    private static final int FIRST_POSITION = 0;

    private HSSFRow row;

    public XlsRow(HSSFRow row) {
        this.row = row;
    }

    public HSSFRow getRowObject() {
        return this.row;
    }

    public int compareTo(Object o) {
        XlsRow xlsRow = (XlsRow) o;
        String value1 = row.getCell(FIRST_POSITION).getStringCellValue();
        String value2 = xlsRow.getRowObject().getCell(FIRST_POSITION).getStringCellValue();
        return value1.compareTo(value2);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        XlsRow xlsRow = (XlsRow) o;
        HSSFCell cell1 = row.getCell(FIRST_POSITION);
        HSSFCell cell2 = xlsRow.getRowObject().getCell(FIRST_POSITION);
        return cell1.getStringCellValue().equals(cell2.getStringCellValue());
    }

    @Override
    public int hashCode() {
        return this.getRowObject().getCell(0).getStringCellValue().hashCode();
    }   

}
